import { ref, onMounted } from 'vue'
import { _getTopCategory } from '@/apis/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export const userCategory = () => {
  const route = useRoute()
  const categoryData = ref({})
  const getTopCategory = async (id = route.params.id) => {
    const { data: res } = await _getTopCategory(id)
    categoryData.value = res.result || {}
  }
  onBeforeRouteUpdate((to) => {
    getTopCategory(to.params.id)
  })
  onMounted(() => {
    getTopCategory()
  })
  return {
    categoryData
  }
}